<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>H5拖拽</title>
    <style>
        html,body{
            /* width: 100%;
            height:100%; */
        }
        #drag{
            width: 150px;
            height: 150px;
            background-color: red;
            position: absolute;
            left:0;
            top:0;
        }
        #progress{
            width: 500px;
            height:500px;
            background-color: blue;
            position: absolute;
            left:500px;
        }

    </style>
</head>
<body>
    <div id="drag" draggable="true">
    </div>
    <div id="progress" ></div>
    <script>
        let drag = document.getElementById("drag");
        let progress = document.getElementById("progress");
        
        drag.ondrag = function(){
            // console.log("drag");
        }
        drag.ondragend = function(){
            // console.log("dragend");
        }
        progress.ondragenter = function(){
            console.log("dragenter");
        }
        progress.ondragover = function(e){
            e.preventDefault();
            console.log("dragover");
        }
        progress.ondragleave = function(){
            console.log("dragleave");
        }
        // 必须将投放的目标对象上的dragover事件的默认行为阻止才能起作用
        progress.ondrop = function(){
            console.log("drop");
        }
        let x = 0;
        let y = 0;
        drag.ondragstart = function(e){
            x = e.x - parseInt(this.style.left || 0);
            y = e.y - parseInt(this.style.top || 0);
        }
        document.body.ondragover = function(e){
            e.preventDefault();
        }
        drag.ondragend = function(e){
            drag.style.left = e.x - x + "px";
            drag.style.top = e.y - y + "px";
        }
    </script>
</body>
</html>